﻿function addtocookie(name,url){
  var req;
  if (window.XMLHttpRequest){
   req = new XMLHttpRequest();
  }else if (window.ActiveXObject)
  {
   req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if(req)
  {
   req.open("GET","./seturl/setcookie.php?name="+name+"&url="+url,true);
   req.send(null); 
  }
}

function del(){
        target = window.event.srcElement;
        div = target.parentNode;
	//get name and url
        name = div.firstChild.innerHTML;
        url = div.firstChild.getAttribute('href');
	//remove cookie
        var req;
        if (window.XMLHttpRequest){
          req = new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        {
          req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if(req)
        {
          req.open("GET","./seturl/delcookie.php?name="+name+"&url="+url,true);
          req.send(null);
        }
	//remove index
        frontlocation = document.getElementsByTagName("a");
        for(i=0;i<frontlocation.length;i++)
        {
          if(frontlocation[i].getAttribute('href') == url)
          {
            rmnode = frontlocation[i].parentNode;
            break;
          }
        }
        rmnode.parentNode.removeChild(rmnode);
	//remove this page
        div.parentNode.removeChild(div);
}

function add(name,url){
	var list = document.getElementById("list");
	var frontlocation = document.getElementById("setcookie");
	var div = document.createElement("li");
	div.innerHTML = '<span href="'+url+'">'+name+'</span><input type="button" onclick="del();">';
	x = list.innerHTML;
	if(x.indexOf(url)<0)
	{
	  list.appendChild(div);
	  newNode = document.createElement("div");
	  newNode.innerHTML = '<a href="'+url+'" target="_blank">'+name+'</a>';
	  frontlocation.appendChild(newNode);
	  addtocookie(name,url);
	}
}

function inputcheck()
{
	name = document.getElementById("addname").value;
	url = document.getElementById("addurl").value;
	if(name=="请输入网站名称"){
		alert("请输入网站名称!");
	}
	else if(url=="请输入网址"){
		alert("请输入网址!");
	}
	else
	{
		if(url.indexOf("http://")!=0)
			url="http://"+url;
		add(name,url);
		document.getElementById("addname").value = "请输入网站名称";
		document.getElementById("addurl").value = "请输入网址";
	}
}

function savesql()
{
	var req;
	if (window.XMLHttpRequest){
		req = new XMLHttpRequest();
	}
	else if (window.ActiveXObject){
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(req){
		req.open("GET","./seturl/savesql.php");
		req.send(null);
	}
	parent.$.fancybox.close();
} 
